// https://leetcode.cn/problems/longest-common-prefix/?envType=problem-list-v2&envId=o01seMu2

// 计算最长公共前缀

pub fn longest_common_prefix(strs: Vec<String>) -> String {
    let mut len = strs[0].len();
    let mut prefix = &strs[0][0..len];
    for s in &strs[1..] {
        while !s.starts_with(prefix) {
            len -= 1;
            prefix = &strs[0][0..len];
        }

        if prefix.len() == 0 {
            break;
        }
    }

    return String::from(prefix);
}

#[test]
fn test_longest_common_prefix() {
    assert_eq!(longest_common_prefix(vec![String::from("flower"), String::from("flow"), String::from("flight")]), String::from("fl"));
    assert_eq!(longest_common_prefix(vec![String::from("dog"), String::from("racecar"), String::from("car")]), String::from(""));
}
